//
//  WiteBoxAppDelegate.m
//  WiteBox
//
//  Created by John Blanco on 6/26/09.
//  Copyright Effective UI 2009. All rights reserved.
//

#import "WiteBoxAppDelegate.h"
#import "WiteBoxStatisticsViewController.h"
#import "WiteBoxTestsViewController.h"
#import "WiteBoxStyles.h"
#import "TestLocationModel.h"

@implementation WiteBoxAppDelegate

- (void)applicationDidFinishLaunching:(UIApplication *)application {
    window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
	
	[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
	
	// two main views
	witeBoxStatisticsViewController = [[WiteBoxStatisticsViewController alloc] init];
	witeBoxTestsViewController = [[WiteBoxTestsViewController alloc] init];

	witeBoxTestsViewController.witeBoxStatisticsViewController = witeBoxStatisticsViewController;
	
	// split view
	splitController = [[UISplitViewController alloc] init];
	splitController.viewControllers = [NSArray arrayWithObjects:witeBoxTestsViewController,witeBoxStatisticsViewController,nil];
	
	[window addSubview:splitController.view];
	[window makeKeyAndVisible];
	
	// start unit testing thread!
	[self startUnitTesting];
}

- (void)dealloc {
	[witeBoxTestsViewController release];
	[witeBoxStatisticsViewController release];
	[splitController release];
	[window release];
	
	[super dealloc];
}

#pragma mark -

- (void)startUnitTesting {
	[self performSelectorInBackground:@selector(doUnitTesting) withObject:nil];
}

- (void)doUnitTesting {
	NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
	
	TestLocationModel *testLocationModel = [TestLocationModel sharedInstance];

	[testLocationModel setTarget:self action:@selector(unitTestPerformed:)];
	[testLocationModel collectAllTestCaseClasses];
	[testLocationModel scheduleAllTestCases];
	[testLocationModel runAllTestCases];
	
	[pool release];
}

- (void)unitTestPerformed:(NSNumber *)isComplete {
	[witeBoxTestsViewController updateDisplay];
	[witeBoxStatisticsViewController updateDisplay];
}

@end
